<?php

namespace app\stores\controller\admin\cash;

use app\BaseController;
use app\beautiful\model\mall\GoodsModel;
use app\common\model\ProduceDepartModel;
use app\stores\model\cash\CashCategoryModel;
use think\facade\Db;

class GoodsCategory extends BaseController{
    /**
     * 出品部门列表
     */
    public function departList()

    {
        $where['shop_id'] = $this->request->shopId;
        $where['is_delete'] = 0;
        $where['origin_type'] = 2;

        $order=['add_time'=>'desc'];

        $return = ProduceDepartModel::where($where)->field('id,name')->order($order)->select();

        return $this->success($return);

    }
    /**
     * 分类信息
     */
    public function show()

    {
        $category_id = $this->request->param('category_id/d',0);
        $cash_category = CashCategoryModel::info(['category_id'=>$category_id,'shop_id'=>$this->request->shopId,'is_delete'=>0]);
        if(null === $cash_category)  return $this->error('分类不存在');
        return $this->success($cash_category);
    }

    /**
     * 分类列表
     */
    public function list()

    {
        $type = $this->request->param('type/d',0);//1收银台2线上点餐
        $status = $this->request->param('status/d',0);

        if(!in_array($type,[1,2])) return $this->error('分类类型不对');

        $where['shop_id'] = $this->request->shopId;
        $where['type'] = $type;
        if($status!==0) $where['status'] = $status;

        $order=['sort'=>'desc','category_id'=>'desc'];

        $return = CashCategoryModel::list($where,$order,$this->request->page,$this->request->limit);

        return $this->success($return);

    }
    /**
     * 添加/修改分类
    */
    public function saveCategory()

    {
        $name= $this->request->param('name');
        $type = $this->request->param('type/d',0);//1收银台2线上点餐
        $status = $this->request->param('status/d',-1);
        $category_id = $this->request->param('category_id/d',0);
        $is_add_online = $this->request->param('is_add_online/d',0);
        $produce_depart_id = $this->request->param('produce_depart_id/d',0);

        if(empty($name)) return $this->error('请输入名称');
        if($type==1 && $produce_depart_id<1) return $this->error('出品部门不能为空');
        if(!in_array($type,[1,2])) return $this->error('分类类型不对');
        if(!in_array($is_add_online,[0,1])) return $this->error('是否增加线上点餐分类值不正确');

        $cash_category = CashCategoryModel::info(['name'=>$name,'shop_id'=>$this->request->shopId,'is_delete'=>0,'type'=>$type]);
        if($cash_category && $cash_category['category_id']!=$category_id) return $this->error('该分类已存在');

        if($type==1){
            $depart = ProduceDepartModel::getById(['id'=>$produce_depart_id,'shop_id'=>$this->request->shopId,'is_delete'=>0]);
            if(empty($depart)) return $this->error('出品部门不存在');
        }
        $max_sort = CashCategoryModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0,'type'=>1])->max('sort');

        $data['name'] = $name;

        if($type==1) $data['produce_depart_id'] = $produce_depart_id;

        if($category_id<1) $data['type'] = $type;//修改时不能修改类别

        if($category_id<1) $data['sort'] = $max_sort + 1;//修改时不能修改

        if($category_id<1 || ($category_id>0 && $status>0)) $data['status'] = $status;//新增（默认禁用）和修改状态为启用时才修改状态

        Db::startTrans();
        try{
            if($category_id != 0) {
                if(null === $cash_category = CashCategoryModel::info(['category_id'=>$category_id,'shop_id'=>$this->request->shopId,'is_delete'=>0]))  return $this->error('分类不存在');
                CashCategoryModel::up($data,['category_id'=>$category_id]);
            }else{
                //同步添加线上点餐判断是否已存在
                if($type==1 && $is_add_online==1){
                    $online_category = CashCategoryModel::info(['name'=>$name,'shop_id'=>$this->request->shopId,'is_delete'=>0,'type'=>2]);
                    if($online_category){
                        return $this->error('线上点餐菜品分类已存在');
                    }
                }

                $data['shop_id'] = $this->request->shopId;
                $data['add_ip']=request()->ip();
                $data['add_time']=date('Y-m-d H:i:s');
                $data['edit_time']=date('Y-m-d H:i:s');
                CashCategoryModel::insertGetId($data);

                if($type==1 && $is_add_online==1){
                    $max_sort = CashCategoryModel::where(['shop_id'=>$this->request->shopId,'is_delete'=>0,'type'=>2])->max('sort');
                    $online_category = new CashCategoryModel();
                    $online_category->name = $name;
                    $online_category->type = 2;
                    $online_category->sort = $max_sort + 1;
                    $online_category->status = $status;
                    $online_category->shop_id = $this->request->shopId;
                    $online_category->save();
                }
            }
            Db::commit();
            return $this->success('保存成功');
        } catch (\Exception $e){
            Db::rollback();
            return $this->error($e->getMessage());
        }
    }
    /**
     * 修改状态
    */
    public function saveStatus()

    {

        $category_id = $this->request->param('category_id/d',0);
        $status = $this->request->param('status/d',0);

        $cash_category = CashCategoryModel::info(['category_id'=>$category_id,'shop_id'=>$this->request->shopId,'is_delete'=>0]);
        if(null === $cash_category)  return $this->error('分类不存在');

        if(!in_array($status,[1,-1])) return $this->error('状态不正确');

        CashCategoryModel::up(['status'=>$status],['category_id'=>$category_id]);

        return $this->success('保存成功');

    }
    /**
     * 排序
     */
    public function sort(){
        $category_id = $this->request->param('category_id/d',0);
        $sort_type = $this->request->param('sort_type/d',0);

        if($category_id<1) return $this->error('分类ID错误');

        if(!in_array($sort_type,[1,2])) return $this->error('排序类型不对');

        $cash_category = CashCategoryModel::info(['category_id'=>$category_id,'shop_id'=>$this->request->shopId,'is_delete'=>0]);
        if(null === $cash_category)  return $this->error('分类不存在');

        if($sort_type==1){
            $sort = $cash_category['sort'] + 1;
        }else{
            $sort = $cash_category['sort'] - 1;
        }

        $edit_cash_category = CashCategoryModel::info(['shop_id'=>$this->request->shopId,'is_delete'=>0,'sort'=>$sort]);
        if(null === $edit_cash_category)  return $this->error('分类不存在');

        Db::startTrans();
        try{
            CashCategoryModel::up(['sort'=>$edit_cash_category['sort']],['category_id'=>$category_id]);
            CashCategoryModel::up(['sort'=>$cash_category['sort']],['category_id'=>$edit_cash_category['category_id']]);
            Db::commit();
            return $this->success('设置成功');
        } catch (\Exception $e){
            Db::rollback();
            return $this->error($e->getMessage());
        }

    }
    /**
     * 线上分类上传图片
     */
    public function icon(){
        $icon = $this->request->param('icon');
        $top_icon = $this->request->param('top_icon');
        $category_id = $this->request->param('category_id/d',0);

        if($category_id<1) return $this->error('分类ID错误');

        $cash_category = CashCategoryModel::info(['category_id'=>$category_id,'shop_id'=>$this->request->shopId,'is_delete'=>0,'type'=>2]);
        if(null === $cash_category)  return $this->error('分类不存在');
        $update=[];
        if(!empty($icon)) $update['icon'] = $icon;
        if(!empty($top_icon)) $update['top_icon'] = $top_icon;
        CashCategoryModel::up($update,['category_id'=>$category_id]);

        return $this->success('保存成功');
    }
    /**
     * 删除
     */
    public function del()

    {

        $category_id=$this->request->param('category_id/d',0);

        if($category_id<1) return $this->error('分类ID错误');

        $cash_category = CashCategoryModel::info(['category_id'=>$category_id,'shop_id'=>$this->request->shopId,'is_delete'=>0]);

        if(null === $cash_category)  return $this->error('分类不存在');

        $str = $cash_category['type']==1?'cash_category_id':'online_category_id';

        if(null != GoodsModel::where([

                ['is_delete','=',0],

                ['shop_id','=',$this->request->shopId],

                [$str,'=',$category_id],

                ['origin_type','=',2]

            ])->find()){

            return $this->error('该分类下面还有商品未删除');

        }

        CashCategoryModel::up(['is_delete'=>1],['category_id'=>$category_id]);

        return $this->success('删除成功');

    }

}

